﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Show_ThreePhaseCircuit
{
    public partial class MainFrame : Form
    {
        public MainFrame()
        {
            InitializeComponent();
        }

        private void toolStripButtonDeviceList_Click(object sender, EventArgs e)
        {
            ShowForm("DeviceList");
        }

        private void toolStripButtonChannelsList_Click(object sender, EventArgs e)
        {
            ShowForm("ChannelList");
        }
        SortedList<string, Form> ModuleForms = new SortedList<string, Form>();
        internal void ShowForm(string formname)
        {
            this.ShowForm(formname, null);
        }
        internal void ShowForm(string formname, string param)
        {
            Form form = null;
            string formkey = formname + param;
            if (ModuleForms.ContainsKey(formkey))
            {
                form = ModuleForms[formkey];
                form.BringToFront();
            }
            else
            {
                switch (formname)
                {
                    case "DeviceList":
                        form = new FieldDevice.UI.DeviceListForm();
                        break;
                    case "ChannelList":
                        form = new FieldLib.Comm.CommHubForm();
                        break;
                    default:
                        throw new Exception("Invalid" + formname);
                }
                form.MdiParent = this;
                ModuleForms.Add(formkey, form);
                //form.Dock = DockStyle.Fill;
                //form.FormBorderStyle = FormBorderStyle.None;
                form.FormClosed += new FormClosedEventHandler(form_FormClosed);
                form.Tag = formkey;
                form.Show();
            }
        }
        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            string text = (string)((Form)sender).Tag;
            this.ModuleForms.Remove(text);
        }

        private void MainFrame_Load(object sender, EventArgs e)
        {
            try
            {
                this.ShowForm("DeviceList");
                //this.ShowForm("ChannelList");
            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
        }
    }
}
